import os
from pathlib import Path
import json
import pandas as pd

results_dir = Path(__file__).parent.parent / 'results'
result = {}

datasets = os.listdir(results_dir)
dataset_dict = {
    "a0910": "ASSIST2009",
    "assistment-2017": "ASSIST2017",
    "junyi": "Junyi",
    "NIPS2020": "NIPS2020",
}

def display(maxVal: float, minVal: float) -> str:
    avg = (maxVal + minVal) * 50
    delta = (maxVal - minVal) * 50
    return "%.2f$\pm$%.2f" % (avg, delta)
df = pd.DataFrame(columns=["Model", "Dataset", "Auc", "Acc", "Rmse"])
for dataset in datasets:
    result[dataset] = {}
    dataset_dir = results_dir / dataset
    models = os.listdir(dataset_dir)
    for model in models:
        result[dataset][model] = {}
        model_dir = dataset_dir / model
        model_results = os.listdir(model_dir)
        m_results = []
        for mr in model_results:
            if mr[0] == 'r':
                m_results.append(mr)
        model_results = sorted(m_results)[-3:]
        min_auc = 100
        max_auc = 0
        min_acc = 100
        max_acc = 0
        min_rmse = 100
        max_rmse = 0
        for file in model_results:
            with open(model_dir / file, "r") as f:
                data = json.load(f)
                auc = data["auc"]
                acc = data["acc"]
                rmse = data["rmse"]
            min_auc = min(min_auc, auc)
            max_auc = max(max_auc, auc)
            min_acc = min(min_acc, acc)
            max_acc = max(max_acc, acc)
            min_rmse = min(min_rmse, rmse)
            max_rmse = max(max_rmse, rmse)

        df.loc[len(df)] = [model, dataset_dict[dataset], display(max_auc, min_auc), display(max_acc, min_acc), display(max_rmse, min_rmse)]

df.to_csv("result.csv")


        
